<?php // no direct access
defined('_JEXEC') or die('Restricted access');
//JHTML::stylesheet ( 'menucss.css', 'modules/mod_virtuemart_category/css/', false );

/* ID for jQuery dropdown */
$ID = str_replace('.', '_', substr(microtime(true), -8, 8));
$js="
//<![CDATA[
jQuery(document).ready(function() {
		jQuery('#VMmenu".$ID." li.VmClose ul').hide();
		jQuery('#VMmenu".$ID." li .VmArrowdown').click(
		function() {

			if (jQuery(this).parent().next('ul').is(':hidden')) {
				jQuery('#VMmenu".$ID." ul:visible').delay(500).slideUp(500,'linear').parents('li').addClass('VmClose').removeClass('VmOpen');
				jQuery(this).parent().next('ul').slideDown(500,'linear');
				jQuery(this).parents('li').addClass('VmOpen').removeClass('VmClose');
			}
		});
	});
//]]>
" ;

//Find VM2 Itemid from Joomla.
$db = &JFactory::getDbo();
$qry = "SELECT `id` FROM #__menu WHERE `link` LIKE \"%com_virtuemart%\" AND `client_id` =0 ";
$db->setQuery($qry);
$db->query();
$modules = $db->loadResult();

$ItemIdString = "";
if ($modules) {
	$ItemIdString = "&Itemid=".$modules;
}


		$document = JFactory::getDocument();
		$document->addScriptDeclaration($js);?>

<div class="row">
<?php

$max_num_cols = 3; //the maximum amount of columns that can be output
$min_items_per_col = 5; //The minimum amount of items per column
$col_limit_calculation = $max_num_cols * $min_items_per_col; //used to determine which value to chunk by
$num_rows = count($categories); //total number of items in the array


$chunk_count = ($num_rows >= $col_limit_calculation) ? ceil(($num_rows / $max_num_cols)) : $min_items_per_col;

$alphabet_chunk_array = array_chunk($categories, $chunk_count);

foreach ($alphabet_chunk_array as $key=>$categories):
	?>
	<div class="VMmenu<?php echo $class_sfx ?> column<?php echo $key+1;?> span3" id="<?php echo "VMmenu".$ID ?>" >
		<ul class="categories">
			<?php foreach ($categories as $key2=>$category): 
				$active_menu = 'class="VmClose"';
				$caturl = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$category->virtuemart_category_id.$ItemIdString);
				$cattext = $category->category_name;
				//if ($active_category_id == $category->virtuemart_category_id) $active_menu = 'class="active"';
				if (in_array( $category->virtuemart_category_id, $parentCategories)) $active_menu = 'class="VmOpen"';
				
			?>
				
			<li <?php echo $active_menu ?>>
				<div>
					<?php echo JHTML::link($caturl, $cattext);?>
				</div>
							
			</li>
				
				
			<?php endforeach; ?>
		</ul>
	</div>

<?php
endforeach;

?>
</div>